package com.zw.pm.module.board.dailyoutput;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.gen.entity.Factory;
import com.zw.pm.gen.entity.TaskDaily;
import com.zw.pm.module.config.factory.FactoryService;
import com.zw.pm.module.project.build.ProjectModel;
import com.zw.pm.module.system.log.LogService;
import com.zw.pm.util.BigDecimalUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description: 日产量 控制层
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/board/daily")
public class DailyOutputController extends BaseController {

    @Autowired
    private DailyOutputService dailyOutputService;
    @Autowired
    private LogService logService;
    @Autowired
    private FactoryService factoryService;

    /**
     * 日产量 页面
     *
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"board:daily"})
    public String daily(Model model) {
        model.addAttribute("factories", dailyOutputService.findFactories());
        return "board/daily/page";
    }

    /**
     * 项目列表
     *
     * @param projectModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"board:daily"})
    public JsonResult list(ProjectModel projectModel) {
        startPage();
        List<ProjectModel> list = dailyOutputService.findProjectDaily(projectModel);
        return jsonResult(list);
    }

    /**
     * 录入
     *
     * @param taskDaily taskDaily
     * @return json
     */
    @ResponseBody
    @PostMapping("/input")
    @RequiresPermissions({"board:daily"})
    public JsonResult input(TaskDaily taskDaily) {
        Factory factory = factoryService.getFactory(taskDaily.getFactoryId());

        String operationContent = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(taskDaily.getInputTime());
        operationContent="录入日报表信息：生产厂："+
                factory.getName()+
                "   日期："+dateString;
        if(taskDaily.getAutoWeldNum()!=null){
            operationContent+="   自动生产焊接(件):"+taskDaily.getAutoWeldNum();
        }
        if(taskDaily.getAutoWeldWeight()!=null){
            operationContent+="   自动生产焊接(吨):"+taskDaily.getAutoWeldWeight();
        }
        if(taskDaily.getManualWeldNum()!=null){
            operationContent+="   手动生产焊接(件):"+taskDaily.getManualWeldNum();
        }
        if(taskDaily.getManualWeldWeight()!=null){
            operationContent+="   手动生产焊接(吨):"+taskDaily.getManualWeldWeight();
        }
        if(taskDaily.getNoWeldNum()!=null){
            operationContent+="   生产非焊接(件):"+taskDaily.getNoWeldNum();
        }
        if(taskDaily.getNoWeldWeight()!=null){
            operationContent+= "   生产非焊接(吨):"+taskDaily.getNoWeldWeight();
        }
        if(taskDaily.getStoreNum()!=null){
            operationContent+= "   转储(件):"+taskDaily.getStoreNum();
        }
        if(taskDaily.getStoreWeight()!=null){
            operationContent+= "   转储(吨):"+taskDaily.getStoreWeight();
        }
        if(taskDaily.getRemark()!=null){
            operationContent+=  "   备注:"+taskDaily.getRemark();
        }
        logService.saveLog("录入日报表",operationContent);
        return jsonResult(dailyOutputService.saveTaskDaily(taskDaily));
    }

    /**
     * 查询任务下达工厂
     *
     * @param id 项目ID
     * @return list
     */
    @ResponseBody
    @PostMapping("/taskFactory")
    @RequiresPermissions({"board:daily"})
    public List<Factory> findTaskFactory(Integer id) {
        return dailyOutputService.findTaskFactory(id);
    }

    /**
     * 查询项目日报表
     *
     * @param dailyOutputModel dailyOutputModel
     * @return json
     */
    @ResponseBody
    @PostMapping("/projectDailyList")
    @RequiresPermissions({"board:daily"})
    public JsonResult list(DailyOutputModel dailyOutputModel) {
        startPage();
        List<DailyOutputModel> list = dailyOutputService.findProjectDailyList(dailyOutputModel);
        return jsonResult(list);
    }

    /**
     * 删除
     *
     * @param taskDaily taskDaily
     * @return json
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"board:daily"})
    public JsonResult del(TaskDaily taskDaily) {
        DailyOutputModel taskDailyObj= dailyOutputService.findTaskDaily(taskDaily.getId());
        Factory factory = factoryService.getFactory(taskDailyObj.getFactoryId());

        String operationContent = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(taskDailyObj.getInputTime());
        operationContent="被删除的日报表信息：生产厂："+
                factory.getName()+
                "   日期："+dateString;
        if(taskDailyObj.getAutoWeldNum()!=null){
            operationContent+="   自动生产焊接(件):"+taskDailyObj.getAutoWeldNum();
        }
        if(taskDailyObj.getAutoWeldWeight()!=null){
            operationContent+="   自动生产焊接(吨):"+ BigDecimalUtils.round(taskDailyObj.getAutoWeldWeight());
        }
        if(taskDailyObj.getManualWeldNum()!=null){
            operationContent+="   手动生产焊接(件):"+taskDailyObj.getManualWeldNum();
        }
        if(taskDailyObj.getManualWeldWeight()!=null){
            operationContent+="   手动生产焊接(吨):"+ BigDecimalUtils.round(taskDailyObj.getManualWeldWeight());
        }
        if(taskDailyObj.getNoWeldNum()!=null){
            operationContent+="   生产非焊接(件):"+taskDailyObj.getNoWeldNum();
        }
        if(taskDailyObj.getNoWeldWeight()!=null){
            operationContent+= "   生产非焊接(吨):"+ BigDecimalUtils.round(taskDailyObj.getNoWeldWeight());
        }
        if(taskDailyObj.getStoreNum()!=null){
            operationContent+= "   转储(件):"+taskDailyObj.getStoreNum();
        }
        if(taskDailyObj.getStoreWeight()!=null){
            operationContent+= "   转储(吨):"+ BigDecimalUtils.round(taskDailyObj.getStoreWeight());
        }
        if(taskDailyObj.getRemark()!=null){
            operationContent+=  "   备注:"+taskDailyObj.getRemark();
        }
        logService.saveLog("删除日报表",operationContent);
        return jsonResult(dailyOutputService.delTaskDaily(taskDaily));
    }

    /**
     * 根据ID查询
     *
     * @param id id
     * @return TaskDaily
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"board:daily"})
    public DailyOutputModel id(Integer id) {
        return dailyOutputService.findTaskDaily(id);
    }

    /**
     * 修改日报
     *
     * @param taskDaily taskDaily
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"board:daily"})
    public JsonResult update(TaskDaily taskDaily) {

        DailyOutputModel taskDailyObj= dailyOutputService.findTaskDaily(taskDaily.getId());
        Factory factory = factoryService.getFactory(taskDailyObj.getFactoryId());
        String operationContent = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(taskDaily.getInputTime());
        operationContent+="修改前日报表信息：生产厂："+
                factory.getName()+
                        "   日期："+formatter.format(taskDailyObj.getInputTime());
                if(taskDailyObj.getAutoWeldNum()!=null){
                    operationContent+= "   自动生产焊接(件):"+taskDailyObj.getAutoWeldNum();
                }
                if(taskDailyObj.getAutoWeldWeight()!=null){
                    operationContent+= "   自动生产焊接(吨):"+ BigDecimalUtils.round(taskDailyObj.getAutoWeldWeight());
                }
        if(taskDailyObj.getManualWeldNum()!=null){
            operationContent+= "   手动生产焊接(件):"+taskDailyObj.getManualWeldNum();
        }
        if(taskDailyObj.getManualWeldWeight()!=null){
            operationContent+= "   手动生产焊接(吨):"+ BigDecimalUtils.round(taskDailyObj.getManualWeldWeight());
        }
        if(taskDailyObj.getNoWeldNum()!=null){
            operationContent+= "   生产非焊接(件):"+taskDailyObj.getNoWeldNum();
        }
        if(taskDailyObj.getNoWeldWeight()!=null){
            operationContent+= "   生产非焊接(吨):"+ BigDecimalUtils.round(taskDailyObj.getNoWeldWeight());
        }
        if(taskDailyObj.getStoreWeight()!=null){
            operationContent+= "   转储(件):"+taskDailyObj.getStoreNum();
        }
        if(taskDailyObj.getStoreWeight()!=null){
            operationContent+= "   转储(吨):"+ BigDecimalUtils.round(taskDailyObj.getStoreWeight());
        }
        if(taskDailyObj.getRemark()!=null){
            operationContent+= "   备注:"+taskDailyObj.getRemark();
        }
        factory = factoryService.getFactory(taskDaily.getFactoryId());
        operationContent+="<BR><font color='red'>修改后日报表信息：生产厂："+
                factory.getName()+
                "   日期："+dateString;
        if(taskDaily.getAutoWeldNum()!=null){
            operationContent+="   自动生产焊接(件):"+taskDaily.getAutoWeldNum();
        }
        if(taskDaily.getAutoWeldWeight()!=null){
            operationContent+="   自动生产焊接(吨):"+ BigDecimalUtils.round(taskDaily.getAutoWeldWeight());
        }
        if(taskDaily.getManualWeldNum()!=null){
            operationContent+="   手动生产焊接(件):"+taskDaily.getManualWeldNum();
        }
        if(taskDaily.getManualWeldWeight()!=null){
            operationContent+="   手动生产焊接(吨):"+ BigDecimalUtils.round(taskDaily.getManualWeldWeight());
        }
        if(taskDaily.getNoWeldNum()!=null){
            operationContent+="   生产非焊接(件):"+taskDaily.getNoWeldNum();
        }
        if(taskDaily.getNoWeldWeight()!=null){
            operationContent+= "   生产非焊接(吨):"+ BigDecimalUtils.round(taskDaily.getNoWeldWeight());
        }
        if(taskDaily.getStoreNum()!=null){
            operationContent+= "   转储(件):"+taskDaily.getStoreNum();
        }
        if(taskDaily.getStoreWeight()!=null){
            operationContent+= "   转储(吨):"+ BigDecimalUtils.round(taskDaily.getStoreWeight());
        }
        if(taskDaily.getRemark()!=null){
            operationContent+=  "   备注:"+taskDaily.getRemark();
        }
        operationContent+="</font>";
                logService.saveLog("修改日报表信息",operationContent);
        return jsonResult(dailyOutputService.updateTaskDaily(taskDaily));
    }


}
